home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 14 / Example 14.1 / terrain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-16  |  35.9 KB  |  1,298 lines

  1. #include "terrain.h"
  2. #include "camera.h"
  3. #include "mapObject.h"
  4.  
  5. const DWORD TERRAINVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2;
  6.  
  7. //////////////////////////////////////////////////////////////////////////////////////////
  8. //                                    PATCH                                                //
  9. //////////////////////////////////////////////////////////////////////////////////////////
  10.  
  11. PATCH::PATCH()
  12. {
  13.     m_pDevice = NULL;
  14.     m_pMesh = NULL;
  15. }
  16. PATCH::~PATCH()
  17. {
  18.     Release();
  19. }
  20.  
  21. void PATCH::Release()
  22. {
  23.     if(m_pMesh != NULL)
  24.         m_pMesh->Release();
  25.     m_pMesh = NULL;
  26. }
  27.  
  28. HRESULT PATCH::CreateMesh(TERRAIN &ter, RECT source, IDirect3DDevice9* Dev)
  29. {
  30.     if(m_pMesh != NULL)
  31.     {
  32.         m_pMesh->Release();
  33.         m_pMesh = NULL;
  34.     }
  35.  
  36.     try
  37.     {
  38.         m_pDevice = Dev;
  39.         m_mapRect = source;
  40.  
  41.         int width = source.right - source.left;
  42.         int height = source.bottom - source.top;
  43.         int nrVert = (width + 1) * (height + 1);
  44.         int nrTri = width * height * 2;
  45.  
  46.         if(FAILED(D3DXCreateMeshFVF(nrTri, nrVert, D3DXMESH_MANAGED, TERRAINVertex::FVF, m_pDevice, &m_pMesh)))
  47.         {
  48.             debug.Print("Couldn't create mesh for PATCH");
  49.             return E_FAIL;
  50.         }
  51.  
  52.         m_BBox.max = D3DXVECTOR3(-10000.0f, -10000.0f, -10000.0f);
  53.         m_BBox.min = D3DXVECTOR3(10000.0f, 10000.0f, 10000.0f);
  54.  
  55.         //Create vertices
  56.         TERRAINVertex* ver = 0;
  57.         m_pMesh->LockVertexBuffer(0,(void**)&ver);
  58.         for(int z=source.top, z0 = 0;z<=source.bottom;z++, z0++)
  59.             for(int x=source.left, x0 = 0;x<=source.right;x++, x0++)
  60.             {
  61.                 MAPTILE *tile = ter.GetTile(x, z);
  62.  
  63.                 D3DXVECTOR3 pos = D3DXVECTOR3(x, tile->m_height, -z);
  64.                 D3DXVECTOR2 alphaUV = D3DXVECTOR2(x / (float)ter.m_size.x, z / (float)ter.m_size.y);        //Alpha UV
  65.                 D3DXVECTOR2 colorUV = alphaUV * 8.0f;                                                    //Color UV
  66.  
  67.                 ver[z0 * (width + 1) + x0] = TERRAINVertex(pos, ter.GetNormal(x, z), alphaUV, colorUV);
  68.  
  69.                 //Calculate bounding box bounds...
  70.                 if(pos.x < m_BBox.min.x)m_BBox.min.x = pos.x;
  71.                 if(pos.x > m_BBox.max.x)m_BBox.max.x = pos.x;
  72.                 if(pos.y < m_BBox.min.y)m_BBox.min.y = pos.y;
  73.                 if(pos.y > m_BBox.max.y)m_BBox.max.y = pos.y;
  74.                 if(pos.z < m_BBox.min.z)m_BBox.min.z = pos.z;
  75.                 if(pos.z > m_BBox.max.z)m_BBox.max.z = pos.z;
  76.             }
  77.         m_pMesh->UnlockVertexBuffer();
  78.  
  79.         //Calculate Indices
  80.         WORD* ind = 0;
  81.         m_pMesh->LockIndexBuffer(0,(void**)&ind);    
  82.         int index = 0;
  83.  
  84.         for(int z=source.top, z0 = 0;z<source.bottom;z++, z0++)
  85.             for(int x=source.left, x0 = 0;x<source.right;x++, x0++)
  86.             {
  87.                 //Triangle 1
  88.                 ind[index++] =   z0   * (width + 1) + x0;
  89.                 ind[index++] =   z0   * (width + 1) + x0 + 1;
  90.                 ind[index++] = (z0+1) * (width + 1) + x0;        
  91.  
  92.                 //Triangle 2
  93.                 ind[index++] = (z0+1) * (width + 1) + x0;
  94.                 ind[index++] =   z0   * (width + 1) + x0 + 1;
  95.                 ind[index++] = (z0+1) * (width + 1) + x0 + 1;
  96.             }
  97.  
  98.         m_pMesh->UnlockIndexBuffer();
  99.  
  100.         //Set Attributes
  101.         DWORD *att = 0, a = 0;
  102.         m_pMesh->LockAttributeBuffer(0,&att);
  103.         memset(att, 0, sizeof(DWORD)*nrTri);
  104.         m_pMesh->UnlockAttributeBuffer();
  105.     }
  106.     catch(...)
  107.     {
  108.         debug.Print("Error in PATCH::CreateMesh()");
  109.         return E_FAIL;
  110.     }
  111.  
  112.     return S_OK;
  113. }
  114.  
  115. void PATCH::Render()
  116. {
  117.     //Draw mesh
  118.     if(m_pMesh != NULL)
  119.         m_pMesh->DrawSubset(0);
  120. }
  121.  
  122. //////////////////////////////////////////////////////////////////////////////////////////
  123. //                                    TERRAIN                                                //
  124. //////////////////////////////////////////////////////////////////////////////////////////
  125.  
  126. TERRAIN::TERRAIN()
  127. {
  128.     m_pDevice = NULL;
  129.     m_pMapTiles = NULL;
  130.     m_pVisitedTiles = m_pVisibleTiles = NULL;
  131.     m_pLandScape = m_pLightMap = m_pFogOfWarTexture = NULL;
  132. }
  133.  
  134. void TERRAIN::Init(IDirect3DDevice9* Dev, INTPOINT _size)
  135. {
  136.     m_pDevice = Dev;
  137.     m_size = _size;
  138.     m_pHeightMap = NULL;
  139.     m_updateSight = true;
  140.  
  141.     if(m_pMapTiles != NULL)    //Clear old maptiles
  142.         delete [] m_pMapTiles;
  143.  
  144.     //Create maptiles
  145.     m_pMapTiles = new MAPTILE[m_size.x * m_size.y];
  146.     memset(m_pMapTiles, 0, sizeof(MAPTILE)*m_size.x*m_size.y);
  147.  
  148.     //Clear old textures
  149.     for(int i=0;i<m_diffuseMaps.size();i++)
  150.         m_diffuseMaps[i]->Release();
  151.     m_diffuseMaps.clear();
  152.  
  153.     //Load textures
  154.     IDirect3DTexture9* grass = NULL, *mount = NULL, *snow = NULL;
  155.     if(FAILED(D3DXCreateTextureFromFile(Dev, "textures/grass.jpg", &grass)))debug.Print("Could not load grass.jpg");
  156.     if(FAILED(D3DXCreateTextureFromFile(Dev, "textures/mountain.jpg", &mount)))debug.Print("Could not load mountain.jpg");
  157.     if(FAILED(D3DXCreateTextureFromFile(Dev, "textures/snow.jpg", &snow)))debug.Print("Could not load snow.jpg");
  158.     m_diffuseMaps.push_back(grass);
  159.     m_diffuseMaps.push_back(mount);
  160.     m_diffuseMaps.push_back(snow);
  161.     m_pAlphaMap = NULL;
  162.     m_pLightMap = NULL;
  163.  
  164.     //Fog-of-War texture
  165.     if(FAILED(m_pDevice->CreateTexture(256, 256, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFogOfWarTexture, NULL)))debug.Print("Failed to create texture: m_pFogOfWarTexture");
  166.  
  167.     //Terrain Texture
  168.     if(FAILED(m_pDevice->CreateTexture(256, 256, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pLandScape, NULL)))debug.Print("Failed to create texture: m_pLandScape");
  169.  
  170.     // Init font
  171.     D3DXCreateFont(m_pDevice, 40, 0, 0, 1, false,  
  172.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  173.                    DEFAULT_PITCH | FF_DONTCARE, "Arial Black", &m_pProgressFont);
  174.  
  175.     Progress("Creating Terrain", 0.0f);
  176.  
  177.     //Load pixel & vertex shaders
  178.     m_dirToSun = D3DXVECTOR3(1.0f, 0.6f, 0.5f);
  179.     D3DXVec3Normalize(&m_dirToSun, &m_dirToSun);
  180.  
  181.     m_terrainPS.Init(Dev, "shaders/terrain.ps", PIXEL_SHADER);
  182.     m_terrainVS.Init(Dev, "shaders/terrain.vs", VERTEX_SHADER);
  183.     m_vsMatW = m_terrainVS.GetConstant("matW");
  184.     m_vsMatVP = m_terrainVS.GetConstant("matVP");
  185.     m_vsDirToSun = m_terrainVS.GetConstant("DirToSun");
  186.  
  187.     m_objectsPS.Init(Dev, "shaders/objects.ps", PIXEL_SHADER);
  188.     m_objectsVS.Init(Dev, "shaders/objects.vs", VERTEX_SHADER);
  189.     m_objMatW = m_objectsVS.GetConstant("matW");
  190.     m_objMatVP = m_objectsVS.GetConstant("matVP");
  191.     m_objDirToSun = m_objectsVS.GetConstant("DirToSun");
  192.     m_objMapSize = m_objectsVS.GetConstant("mapSize");
  193.  
  194.     //Create white material    
  195.     m_mtrl.Ambient = m_mtrl.Specular = m_mtrl.Diffuse  = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
  196.     m_mtrl.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  197.  
  198.     //Create visited & visible tiles
  199.     if(m_pVisitedTiles)delete [] m_pVisitedTiles;
  200.     if(m_pVisibleTiles)delete [] m_pVisibleTiles;
  201.     m_pVisitedTiles = new bool[m_size.x * m_size.y];
  202.     m_pVisibleTiles = new bool[m_size.x * m_size.y];
  203.     memset(m_pVisitedTiles, false, sizeof(bool)* m_size.x * m_size.y);
  204.     memset(m_pVisibleTiles, false, sizeof(bool)* m_size.x * m_size.y);
  205.  
  206.     Progress("Creating Terrain", 0.4f);
  207.  
  208.     GenerateRandomTerrain(15);
  209. }
  210.  
  211. void TERRAIN::Release()
  212. {
  213.     try
  214.     {
  215.         for(int i=0;i<m_patches.size();i++)
  216.             if(m_patches[i] != NULL)
  217.                 m_patches[i]->Release();
  218.  
  219.         m_patches.clear();
  220.  
  221.         if(m_pHeightMap != NULL)
  222.         {
  223.             m_pHeightMap->Release();
  224.             delete m_pHeightMap;
  225.             m_pHeightMap = NULL;
  226.         }
  227.  
  228.         m_objects.clear();
  229.     }
  230.     catch(...)
  231.     {
  232.         debug.Print("Error in TERRAIN::Release()");
  233.     }
  234. }
  235.  
  236. void TERRAIN::GenerateRandomTerrain(int numPatches)
  237. {
  238.     try
  239.     {
  240.         Release();
  241.  
  242.         //Create two heightmaps and multiply them
  243.         m_pHeightMap = new HEIGHTMAP(m_size, 20.0f);
  244.         HEIGHTMAP hm2(m_size, 2.0f);
  245.         HEIGHTMAP hm3(m_size, 2.0f);
  246.  
  247.         m_pHeightMap->CreateRandomHeightMap(rand()%2000, 1.0f, 0.7f, 7);
  248.         hm2.CreateRandomHeightMap(rand()%2000, 2.5f, 0.8f, 3);
  249.  
  250.         //Load 4 player filter
  251.         hm3.LoadFromFile(m_pDevice, "heightmaps/four_players.jpg");
  252.         hm2.Cap(hm2.m_maxHeight * 0.4f);
  253.  
  254.         *m_pHeightMap *= hm2;
  255.         *m_pHeightMap *= hm3;
  256.         hm2.Release();        
  257.         hm3.Release();
  258.  
  259.         //Add objects
  260.         HEIGHTMAP hm4(m_size, 1.0f);
  261.         hm4.CreateRandomHeightMap(rand()%1000, 5.5f, 0.9f, 7);
  262.  
  263.         for(int y=0;y<m_size.y;y++)
  264.             for(int x=0;x<m_size.x;x++)
  265.             {
  266.                 if(m_pHeightMap->GetHeight(x, y) == 0.0f && hm4.GetHeight(x, y) > 0.7f && rand()%12 == 0)
  267.                     AddObject(0, INTPOINT(x, y));    //Tree
  268.                 else if(m_pHeightMap->GetHeight(x, y) >= 1.0f && hm4.GetHeight(x, y) > 0.9f && rand()%30 == 0)
  269.                     AddObject(1, INTPOINT(x, y));    //Stone
  270.             }
  271.  
  272.         hm4.Release();        
  273.  
  274.         InitPathfinding();
  275.         CreatePatches(numPatches);
  276.  
  277.         CalculateAlphaMaps();
  278.         CalculateLightMap(false);
  279.  
  280.         RenderLandscape();
  281.     }
  282.     catch(...)
  283.     {
  284.         debug.Print("Error in TERRAIN::GenerateRandomTerrain()");
  285.     }
  286. }
  287.  
  288. void TERRAIN::CreatePatches(int numPatches)
  289. {
  290.     try
  291.     {
  292.         //Clear any old patches
  293.         for(int i=0;i<m_patches.size();i++)
  294.             if(m_patches[i] != NULL)
  295.                 m_patches[i]->Release();
  296.         m_patches.clear();
  297.  
  298.         //Create new patches
  299.         for(int y=0;y<numPatches;y++)
  300.         {
  301.             Progress("Creating Terrain Mesh", y / (float)numPatches);
  302.  
  303.             for(int x=0;x<numPatches;x++)
  304.             {
  305.                 RECT r = {x * (m_size.x - 1) / (float)numPatches, 
  306.                         y * (m_size.y - 1) / (float)numPatches, 
  307.                         (x+1) * (m_size.x - 1) / (float)numPatches,
  308.                         (y+1) * (m_size.y - 1) / (float)numPatches};
  309.                         
  310.                 PATCH *p = new PATCH();
  311.                 p->CreateMesh(*this, r, m_pDevice);
  312.                 m_patches.push_back(p);
  313.             }
  314.         }
  315.     }
  316.     catch(...)
  317.     {
  318.         debug.Print("Error in TERRAIN::CreatePatches()");
  319.     }
  320. }
  321.  
  322. void TERRAIN::CalculateAlphaMaps()
  323. {
  324.     Progress("Creating Alpha Map", 0.0f);
  325.  
  326.     //Clear old alpha maps
  327.     if(m_pAlphaMap != NULL)
  328.         m_pAlphaMap->Release();
  329.  
  330.     //Create new alpha map
  331.     D3DXCreateTexture(m_pDevice, 128, 128, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pAlphaMap);
  332.  
  333.     //Lock the texture
  334.     D3DLOCKED_RECT sRect;
  335.     m_pAlphaMap->LockRect(0, &sRect, NULL, NULL);
  336.     BYTE *bytes = (BYTE*)sRect.pBits;
  337.     memset(bytes, 0, 128*sRect.Pitch);        //Clear texture to black
  338.  
  339.     for(int i=0;i<m_diffuseMaps.size();i++)
  340.         for(int y=0;y<sRect.Pitch / 4;y++)
  341.             for(int x=0;x<sRect.Pitch / 4;x++)
  342.             {
  343.                 int terrain_x = m_size.x * (x / (float)(sRect.Pitch / 4.0f));
  344.                 int terrain_y = m_size.y * (y / (float)(sRect.Pitch / 4.0f));
  345.                 MAPTILE *tile = GetTile(terrain_x, terrain_y);
  346.  
  347.                 if(tile != NULL && tile->m_type == i)
  348.                     bytes[y * sRect.Pitch + x * 4 + i] = 255;
  349.             }
  350.  
  351.     //Unlock the texture
  352.     m_pAlphaMap->UnlockRect(0);
  353.     
  354.     //D3DXSaveTextureToFile("alpha.bmp", D3DXIFF_BMP, m_pAlphaMap, NULL);
  355. }
  356.  
  357. void TERRAIN::CalculateLightMap(bool allWhite)
  358. {
  359.     try
  360.     {
  361.         //Clear old alpha maps
  362.         if(m_pLightMap != NULL)
  363.             m_pLightMap->Release();
  364.  
  365.         //Create new light map
  366.         D3DXCreateTexture(m_pDevice, 128, 128, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, &m_pLightMap);
  367.  
  368.         //Lock the texture
  369.         D3DLOCKED_RECT sRect;
  370.         m_pLightMap->LockRect(0, &sRect, NULL, NULL);
  371.         BYTE *bytes = (BYTE*)sRect.pBits;
  372.         memset(bytes, 255, sRect.Pitch*sRect.Pitch);        //Clear texture to white
  373.  
  374.         if(allWhite)
  375.         {
  376.             m_pLightMap->UnlockRect(0);
  377.             return;
  378.         }
  379.  
  380.         for(int y=0;y<sRect.Pitch;y++)
  381.         {
  382.             Progress("Calculating Lightmap", y / (float)sRect.Pitch);
  383.  
  384.             for(int x=0;x<sRect.Pitch;x++)
  385.             {
  386.                 float terrain_x = (float)m_size.x * (x / (float)(sRect.Pitch));
  387.                 float terrain_z = (float)m_size.y * (y / (float)(sRect.Pitch));
  388.  
  389.                 //Find patch that the terrain_x, terrain_z is over
  390.                 bool done = false;
  391.                 for(int p=0;p<m_patches.size() && !done;p++)
  392.                 {
  393.                     RECT mr = m_patches[p]->m_mapRect;
  394.  
  395.                     //Focus within patch maprect or not?
  396.                     if(terrain_x >= mr.left && terrain_x < mr.right &&
  397.                          terrain_z >= mr.top && terrain_z < mr.bottom)
  398.                     {            
  399.                         // Collect only the closest intersection
  400.                         RAY rayTop(D3DXVECTOR3(terrain_x, 10000.0f, -terrain_z), D3DXVECTOR3(0.0f, -1.0f, 0.0f));
  401.                         float dist = rayTop.Intersect(m_patches[p]->m_pMesh);
  402.  
  403.                         if(dist >= 0.0f)
  404.                         {
  405.                             RAY ray(D3DXVECTOR3(terrain_x, 10000.0f - dist + 0.01f, -terrain_z), m_dirToSun);
  406.  
  407.                             for(int p2=0;p2<m_patches.size() && !done;p2++)
  408.                                 if(ray.Intersect(m_patches[p2]->m_BBox) >= 0)
  409.                                 {
  410.                                     if(ray.Intersect(m_patches[p2]->m_pMesh) >= 0)    //In shadow
  411.                                     {
  412.                                         done = true;
  413.                                         bytes[y * sRect.Pitch + x] = 128;
  414.                                     }
  415.                                 }
  416.  
  417.                             done = true;
  418.                         }
  419.                     }
  420.                 }                        
  421.             }
  422.         }
  423.  
  424.         //Smooth lightmap        
  425.         for(int i=0;i<3;i++)
  426.         {
  427.             Progress("Smoothing the Lightmap", i / 3.0f);
  428.  
  429.             BYTE* tmpBytes = new BYTE[sRect.Pitch * sRect.Pitch];
  430.             memcpy(tmpBytes, sRect.pBits, sRect.Pitch * sRect.Pitch);
  431.  
  432.             for(int y=1;y<sRect.Pitch-1;y++)
  433.                 for(int x=1;x<sRect.Pitch-1;x++)
  434.                 {
  435.                     long index = y*sRect.Pitch + x;
  436.                     BYTE b1 = bytes[index];
  437.                     BYTE b2 = bytes[index - 1];
  438.                     BYTE b3 = bytes[index - sRect.Pitch];
  439.                     BYTE b4 = bytes[index + 1];
  440.                     BYTE b5 = bytes[index + sRect.Pitch];
  441.                     
  442.                     tmpBytes[index] = (BYTE)((b1 + b2 + b3 + b4 + b5) / 5);
  443.                 }
  444.  
  445.             memcpy(sRect.pBits, tmpBytes, sRect.Pitch * sRect.Pitch);
  446.             delete [] tmpBytes;
  447.         }
  448.  
  449.         //Unlock the texture
  450.         m_pLightMap->UnlockRect(0);
  451.         
  452.         //D3DXSaveTextureToFile("light.bmp", D3DXIFF_BMP, m_pLightMap, NULL);
  453.     }
  454.     catch(...)
  455.     {
  456.         debug.Print("Error in TERRAIN::CalculateLightMap()");
  457.     }
  458. }
  459.  
  460. D3DXVECTOR3 TERRAIN::GetNormal(int x, int y)
  461. {
  462.     //Neighboring map nodes (D, B, C, F, H, G)
  463.     INTPOINT mp[] = {INTPOINT(x-1, y),   INTPOINT(x, y-1), 
  464.                      INTPOINT(x+1, y-1), INTPOINT(x+1, y),
  465.                        INTPOINT(x, y+1),   INTPOINT(x-1, y+1)};
  466.  
  467.     //if there's an invalid map node return (0, 1, 0)
  468.     if(!Within(mp[0]) || !Within(mp[1]) || !Within(mp[2]) || 
  469.        !Within(mp[3]) || !Within(mp[4]) || !Within(mp[5]))
  470.         return D3DXVECTOR3(0.0f, 1.0f, 0.0f);
  471.  
  472.     //Calculate the normals of the 6 neighboring planes
  473.     D3DXVECTOR3 normal = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  474.  
  475.     for(int i=0;i<6;i++)
  476.     {
  477.         D3DXPLANE plane;
  478.         D3DXPlaneFromPoints(&plane, 
  479.                             &GetWorldPos(INTPOINT(x, y)),
  480.                             &GetWorldPos(mp[i]), 
  481.                             &GetWorldPos(mp[(i + 1) % 6]));
  482.  
  483.         normal +=  D3DXVECTOR3(plane.a, plane.b, plane.c);
  484.     }
  485.  
  486.     D3DXVec3Normalize(&normal, &normal);
  487.     return normal;
  488. }
  489.  
  490. void TERRAIN::AddObject(int type, INTPOINT mappos)
  491. {
  492.     D3DXVECTOR3 pos = D3DXVECTOR3(mappos.x, m_pHeightMap->GetHeight(mappos), -mappos.y);    
  493.     D3DXVECTOR3 rot = D3DXVECTOR3((rand()%1000 / 1000.0f) * 0.13f, (rand()%1000 / 1000.0f) * 3.0f, (rand()%1000 / 1000.0f) * 0.13);
  494.  
  495.     float sca_xz = (rand()%1000 / 1000.0f) * 0.5f + 0.5f;
  496.     float sca_y = (rand()%1000 / 1000.0f) * 1.0f + 0.5f;
  497.     D3DXVECTOR3 sca = D3DXVECTOR3(sca_xz, sca_y, sca_xz);
  498.  
  499.     m_objects.push_back(OBJECT(type, mappos, pos, rot, sca));
  500. }
  501.  
  502. void TERRAIN::Render(CAMERA &camera)
  503. {
  504.     //Set render states        
  505.     m_pDevice->SetRenderState(D3DRS_LIGHTING, false);
  506.     m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, true);    
  507.     
  508.     m_pDevice->SetTexture(0, m_pAlphaMap);
  509.     m_pDevice->SetTexture(1, m_diffuseMaps[0]);        //Grass
  510.     m_pDevice->SetTexture(2, m_diffuseMaps[1]);        //Mountain
  511.     m_pDevice->SetTexture(3, m_diffuseMaps[2]);        //Snow
  512.     m_pDevice->SetTexture(4, m_pFogOfWarTexture);        //Fog-of-War
  513.  
  514.     m_pDevice->SetMaterial(&m_mtrl);
  515.  
  516.     D3DXMATRIX world, vp = camera.GetViewMatrix() * camera.GetProjectionMatrix();
  517.     D3DXMatrixIdentity(&world);
  518.     m_pDevice->SetTransform(D3DTS_WORLD, &world);
  519.     
  520.     //Set vertex shader variables
  521.     m_terrainVS.SetMatrix(m_vsMatW, world);
  522.     m_terrainVS.SetMatrix(m_vsMatVP, vp);
  523.     m_terrainVS.SetVector3(m_vsDirToSun, m_dirToSun);
  524.  
  525.     m_terrainVS.Begin();
  526.     m_terrainPS.Begin();
  527.         
  528.     for(int p=0;p<m_patches.size();p++)
  529.         if(m_patches[p]->m_visible && !camera.Cull(m_patches[p]->m_BBox))
  530.             m_patches[p]->Render();
  531.  
  532.     m_terrainPS.End();
  533.     m_terrainVS.End();
  534.  
  535.     m_pDevice->SetTexture(1, NULL);
  536.     m_pDevice->SetTexture(2, NULL);
  537.     m_pDevice->SetTexture(3, NULL);
  538.     m_pDevice->SetTexture(4, NULL);
  539.  
  540.     //Render Objects
  541.     m_objectsVS.SetMatrix(m_objMatW, world);
  542.     m_objectsVS.SetMatrix(m_objMatVP, vp);
  543.     m_objectsVS.SetVector3(m_objDirToSun, m_dirToSun);
  544.     m_objectsVS.SetVector3(m_objMapSize, D3DXVECTOR3(m_size.x, m_size.y, 0.0f));
  545.  
  546.     m_pDevice->SetTexture(1, m_pFogOfWarTexture);        //Lightmap
  547.     
  548.     m_objectsVS.Begin();
  549.     m_objectsPS.Begin();
  550.  
  551.     for(int i=0;i<m_objects.size();i++)
  552.         if(m_objects[i].m_visible && !camera.Cull(m_objects[i].m_BBox))
  553.         {
  554.             D3DXMATRIX m = m_objects[i].m_meshInstance.GetWorldMatrix();
  555.             m_objectsVS.SetMatrix(m_objMatW, m);
  556.             m_objects[i].Render();
  557.         }
  558.  
  559.     m_objectsVS.End();
  560.     m_objectsPS.End();
  561. }
  562.  
  563. void TERRAIN::Progress(std::string text, float prc)
  564. {
  565.     m_pDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L);
  566.     m_pDevice->BeginScene();
  567.     
  568.     RECT rc = {200, 250, 600, 300};
  569.     m_pProgressFont->DrawText(NULL, text.c_str(), -1, &rc, DT_CENTER | DT_VCENTER | DT_NOCLIP, 0xff000000);
  570.  
  571.     //Progress bar
  572.     D3DRECT r;
  573.     r.x1 = 200; r.x2 = 600;
  574.     r.y1 = 300; r.y2 = 340;
  575.     m_pDevice->Clear(1, &r, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0L);
  576.     r.x1 = 202; r.x2 = 598;
  577.     r.y1 = 302; r.y2 = 338;
  578.     m_pDevice->Clear(1, &r, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L);
  579.     r.x1 = 202; r.x2 = 202 + 396 * prc;
  580.     r.y1 = 302; r.y2 = 338;
  581.     m_pDevice->Clear(1, &r, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 1.0f, 0L);
  582.  
  583.     m_pDevice->EndScene();
  584.     m_pDevice->Present(0, 0, 0, 0);
  585. }
  586.  
  587. void TERRAIN::SetOrthogonalView()
  588. {
  589.     D3DXMATRIX world, view, proj;
  590.  
  591.     //World matrix
  592.     D3DXMatrixIdentity(&world);
  593.  
  594.     //View matrix (eye & focus in the center of the terrain)
  595.     D3DXVECTOR2 center = D3DXVECTOR2((m_size.x - 1) / 2.0f, -(m_size.y - 1) / 2.0f);
  596.  
  597.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(center.x, 1000.0f, center.y),
  598.                               &D3DXVECTOR3(center.x, 0.0f, center.y),
  599.                               &D3DXVECTOR3(0.0f, 0.0f, 1.0f));
  600.  
  601.     //Projection matrix
  602.     D3DXMatrixOrthoLH(&proj, m_size.x-1, m_size.y-1, 0.1f, 2000.0f);
  603.  
  604.     //Set transformation matrices
  605.     m_pDevice->SetTransform(D3DTS_WORLD, &world);
  606.     m_pDevice->SetTransform(D3DTS_VIEW, &view);
  607.     m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);
  608. }
  609.  
  610. void TERRAIN::UpdateSightMatrices(std::vector<MAPOBJECT*> &mapObjects)
  611. {
  612.     memset(m_pVisibleTiles, true, sizeof(bool) * m_size.x * m_size.y);
  613.     memset(m_pVisitedTiles, true, sizeof(bool) * m_size.x * m_size.y);
  614.  
  615.     /*
  616.     memset(m_pVisibleTiles, false, sizeof(bool) * m_size.x * m_size.y);
  617.     for(int i=0;i<mapObjects.size();i++)
  618.         if(mapObjects[i] != NULL)
  619.         {
  620.             int sr = mapObjects[i]->sightRadius, a = 0;
  621.             INTPOINT start = mapObjects[i]->m_mappos - INTPOINT(sr, sr);
  622.             INTPOINT end = mapObjects[i]->m_mappos + INTPOINT(sr, sr);
  623.  
  624.             for(int y=start.y;y<=end.y;y++)
  625.                 for(int x=start.x;x<=end.x;x++)
  626.                     if(x >= 0 && y >= 0 && x < m_size.x && y < m_size.y)
  627.                     {
  628.                         int index = x + y * m_size.x;
  629.                         m_pVisitedTiles[index] = m_pVisibleTiles[index] = true;
  630.                     }
  631.         }
  632.     */
  633.  
  634.     //Calculate visibility variable of Patches
  635.     for(int i=0;i<m_patches.size();i++)
  636.         if(m_patches[i] != NULL)
  637.         {
  638.             m_patches[i]->m_visible = false;
  639.             RECT r = m_patches[i]->m_mapRect;
  640.  
  641.             for(int y=r.top;y<=r.bottom && !m_patches[i]->m_visible;y++)
  642.                 for(int x=r.left;x<=r.right && !m_patches[i]->m_visible;x++)
  643.                     if(m_pVisitedTiles[x + y * m_size.x])
  644.                         m_patches[i]->m_visible = true;
  645.         }
  646.  
  647.     //Calculate visibility of terrain objects
  648.     for(int i=0;i<m_objects.size();i++)
  649.         m_objects[i].m_visible = m_pVisitedTiles[m_objects[i].m_mappos.x + m_objects[i].m_mappos.y * m_size.x];
  650. }
  651.  
  652. void TERRAIN::RenderLandscape()
  653. {
  654.     //Set orthogonal rendering view & projection
  655.     SetOrthogonalView();
  656.  
  657.     //Retrieve the surface of the back buffer
  658.     IDirect3DSurface9 *backSurface = NULL;
  659.     m_pDevice->GetRenderTarget(0, &backSurface);
  660.     
  661.     //Get the surface of the minimap texture
  662.     IDirect3DSurface9 *landScapeSurface = NULL;            
  663.     m_pLandScape->GetSurfaceLevel(0, &landScapeSurface);            
  664.  
  665.     //Set render target to the visible surface
  666.     m_pDevice->SetRenderTarget(0, landScapeSurface);
  667.  
  668.     //Clear render target to black
  669.     m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  670.  
  671.     m_pDevice->BeginScene();
  672.  
  673.     //Render Terrain
  674.     {
  675.         //Set render states        
  676.         m_pDevice->SetRenderState(D3DRS_LIGHTING, false);
  677.         m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, true);
  678.  
  679.         //Create completely white lightmap
  680.         IDirect3DTexture9* white = NULL;
  681.         m_pDevice->CreateTexture(32, 32, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, &white, NULL);
  682.         D3DLOCKED_RECT sRect;
  683.         white->LockRect(0, &sRect, NULL, NULL);
  684.         memset((BYTE*)sRect.pBits, 255, 32 * 32);
  685.         white->UnlockRect(0);
  686.  
  687.         m_pDevice->SetTexture(0, m_pAlphaMap);
  688.         m_pDevice->SetTexture(1, m_diffuseMaps[0]);        //Grass
  689.         m_pDevice->SetTexture(2, m_diffuseMaps[1]);        //Mountain
  690.         m_pDevice->SetTexture(3, m_diffuseMaps[2]);        //Snow
  691.         m_pDevice->SetTexture(4, white);                //Use White Texture
  692.         m_pDevice->SetMaterial(&m_mtrl);
  693.  
  694.         D3DXMATRIX world, view, proj;
  695.         D3DXMatrixIdentity(&world);
  696.         m_pDevice->SetTransform(D3DTS_WORLD, &world);
  697.         m_pDevice->GetTransform(D3DTS_VIEW, &view);
  698.         m_pDevice->GetTransform(D3DTS_PROJECTION, &proj);
  699.         
  700.         //Set vertex shader variables
  701.         m_terrainVS.SetMatrix(m_vsMatW, world);
  702.         m_terrainVS.SetMatrix(m_vsMatVP, view * proj);
  703.         m_terrainVS.SetVector3(m_vsDirToSun, m_dirToSun);
  704.  
  705.         m_terrainVS.Begin();
  706.         m_terrainPS.Begin();
  707.         for(int p=0;p<m_patches.size();p++)
  708.             m_patches[p]->Render();
  709.         m_terrainPS.End();
  710.         m_terrainVS.End();
  711.  
  712.         for(int i=0;i<8;i++)
  713.             m_pDevice->SetTexture(i, NULL);
  714.  
  715.         //Render Objects
  716.         m_objectsVS.SetMatrix(m_objMatW, world);
  717.         m_objectsVS.SetMatrix(m_objMatVP, view * proj);
  718.         m_objectsVS.SetVector3(m_objDirToSun, m_dirToSun);
  719.         m_objectsVS.SetVector3(m_objMapSize, D3DXVECTOR3(m_size.x, m_size.y, 0.0f));
  720.         m_pDevice->SetTexture(1, white);        //White Texture
  721.         
  722.         m_objectsVS.Begin();
  723.         m_objectsPS.Begin();
  724.  
  725.         for(int i=0;i<m_objects.size();i++)
  726.         {
  727.             D3DXMATRIX m = m_objects[i].m_meshInstance.GetWorldMatrix();
  728.             m_objectsVS.SetMatrix(m_objMatW, m);
  729.             m_objects[i].Render();
  730.         }
  731.  
  732.         m_objectsVS.End();
  733.         m_objectsPS.End();
  734.  
  735.         white->Release();
  736.     }
  737.  
  738.     m_pDevice->EndScene();
  739.  
  740.     //Reset render target to back buffer
  741.     m_pDevice->SetRenderTarget(0, backSurface);
  742.  
  743.     //Release surfaces
  744.     landScapeSurface->Release();
  745.     backSurface->Release();
  746.  
  747.     //D3DXSaveTextureToFile("landScape.bmp", D3DXIFF_BMP, m_pLandScape, NULL);
  748. }
  749.  
  750. bool TERRAIN::Within(INTPOINT p)
  751. {
  752.     return p.x >= 0 && p.y >= 0 && p.x < m_size.x && p.y < m_size.y;
  753. }
  754.  
  755. void TERRAIN::InitPathfinding()
  756. {
  757.     try
  758.     {
  759.         //Read maptile heights & types from heightmap
  760.         for(int y=0;y<m_size.y;y++)
  761.             for(int x=0;x<m_size.x;x++)
  762.             {
  763.                 MAPTILE *tile = GetTile(x, y);
  764.                 if(m_pHeightMap != NULL)tile->m_height = m_pHeightMap->GetHeight(x, y);
  765.                 tile->m_mappos = INTPOINT(x, y);
  766.                 
  767.                 if(tile->m_height < 0.3f)         tile->m_type = 0;    //Grass
  768.                 else if(tile->m_height < 7.0f) tile->m_type = 1;    //Stone
  769.                 else                         tile->m_type = 2;    //Snow
  770.             }
  771.  
  772.         //Calculate tile cost as a function of the height variance
  773.         for(int y=0;y<m_size.y;y++)        
  774.             for(int x=0;x<m_size.x;x++)
  775.             {
  776.                 MAPTILE *tile = GetTile(x, y);
  777.  
  778.                 if(tile != NULL)
  779.                 {
  780.                     //Possible neighbors
  781.                     INTPOINT p[] = {INTPOINT(x-1, y-1), INTPOINT(x, y-1), INTPOINT(x+1, y-1),
  782.                                     INTPOINT(x-1, y),                      INTPOINT(x+1, y),
  783.                                     INTPOINT(x-1, y+1), INTPOINT(x, y+1), INTPOINT(x+1, y+1)};
  784.  
  785.                     float variance = 0.0f;
  786.                     int nr = 0;
  787.  
  788.                     //For each neighbor
  789.                     for(int i=0;i<8;i++)    
  790.                         if(Within(p[i]))
  791.                         {
  792.                             MAPTILE *neighbor = GetTile(p[i]);
  793.  
  794.                             if(neighbor != NULL)
  795.                             {
  796.                                 float v = neighbor->m_height - tile->m_height;
  797.                                 variance += (v * v);
  798.                                 nr++;
  799.                             }
  800.                         }
  801.  
  802.                     //Cost = height variance
  803.                     variance /= (float)nr;
  804.                     tile->m_cost = variance + 0.1f;
  805.                     if(tile->m_cost > 1.0f)tile->m_cost = 1.0f;
  806.  
  807.                     //If the tile cost is less than 1.0f, then we can walk on the tile
  808.                     tile->m_walkable = tile->m_cost < 0.5f;
  809.                 }
  810.             }
  811.  
  812.         //Make maptiles with objects on them not walkable
  813.         for(int i=0;i<m_objects.size();i++)
  814.         {
  815.             MAPTILE *tile = GetTile(m_objects[i].m_mappos);
  816.             if(tile != NULL)
  817.             {
  818.                 tile->m_walkable = false;
  819.                 tile->m_cost = 1.0f;
  820.             }
  821.         }
  822.  
  823.         //Connect maptiles using the neightbors[] pointers
  824.         for(int y=0;y<m_size.y;y++)        
  825.             for(int x=0;x<m_size.x;x++)
  826.             {
  827.                 MAPTILE *tile = GetTile(x, y);
  828.                 if(tile != NULL && tile->m_walkable)
  829.                 {
  830.                     //Clear old connections
  831.                     for(int i=0;i<8;i++)
  832.                         tile->neighbors[i] = NULL;
  833.  
  834.                     //Possible neighbors
  835.                     INTPOINT p[] = {INTPOINT(x-1, y-1), INTPOINT(x, y-1), INTPOINT(x+1, y-1),
  836.                                     INTPOINT(x-1, y),                      INTPOINT(x+1, y),
  837.                                     INTPOINT(x-1, y+1), INTPOINT(x, y+1), INTPOINT(x+1, y+1)};
  838.  
  839.                     //For each neighbor
  840.                     for(int i=0;i<8;i++)    
  841.                         if(Within(p[i]))
  842.                         {
  843.                             MAPTILE *neighbor = GetTile(p[i]);
  844.  
  845.                             //Connect tiles if the neighbor is walkable
  846.                             if(neighbor != NULL && neighbor->m_walkable)
  847.                                 tile->neighbors[i] = neighbor;
  848.                         }
  849.                 }
  850.             }
  851.  
  852.         CreateTileSets();
  853.     }
  854.     catch(...)
  855.     {
  856.         debug.Print("Error in InitPathfinding()");
  857.     }    
  858. }
  859.  
  860. void TERRAIN::UpdatePathfinding(RECT *r)
  861. {
  862.     if(r == NULL)
  863.     {
  864.         InitPathfinding();
  865.         return;
  866.     }
  867.  
  868.     //Connect maptiles using the neightbors[] pointers
  869.     for(int y=r->top; y<=r->bottom; y++)        
  870.         for(int x=r->left; x<=r->right; x++)
  871.         {
  872.             MAPTILE *tile = GetTile(x, y);
  873.             if(tile != NULL && tile->m_walkable)
  874.             {
  875.                 //Clear old connections
  876.                 for(int i=0;i<8;i++)
  877.                     tile->neighbors[i] = NULL;
  878.  
  879.                 //Possible neighbors
  880.                 INTPOINT p[] = {INTPOINT(x-1, y-1), INTPOINT(x, y-1), INTPOINT(x+1, y-1),
  881.                                 INTPOINT(x-1, y),                      INTPOINT(x+1, y),
  882.                                 INTPOINT(x-1, y+1), INTPOINT(x, y+1), INTPOINT(x+1, y+1)};
  883.  
  884.                 //For each neighbor
  885.                 for(int i=0;i<8;i++)
  886.                     if(Within(p[i]))
  887.                     {
  888.                         MAPTILE *neighbor = GetTile(p[i]);
  889.  
  890.                         //Connect tiles if the neighbor is walkable
  891.                         if(neighbor != NULL && neighbor->m_walkable)
  892.                             tile->neighbors[i] = neighbor;
  893.                     }
  894.             }
  895.         }
  896.  
  897.     CreateTileSets();
  898. }
  899.  
  900. void TERRAIN::CreateTileSets()
  901. {
  902.     try
  903.     {
  904.         int setNo = 0;
  905.         for(int y=0;y<m_size.y;y++)        //Set a unique set for each tile...
  906.             for(int x=0;x<m_size.x;x++)
  907.                 m_pMapTiles[x + y * m_size.x].m_set = setNo++;
  908.  
  909.         bool changed = true;
  910.  
  911.         while(changed)
  912.         {
  913.             changed = false;
  914.  
  915.             for(int y=0;y<m_size.y;y++)
  916.                 for(int x=0;x<m_size.x;x++)
  917.                 {
  918.                     MAPTILE *tile = GetTile(x, y);
  919.  
  920.                     //Find the lowest set of a neighbor
  921.                     if(tile != NULL && tile->m_walkable)
  922.                     {
  923.                         for(int i=0;i<8;i++)
  924.                             if(tile->neighbors[i] != NULL &&
  925.                                 tile->neighbors[i]->m_walkable &&
  926.                                 tile->neighbors[i]->m_set < tile->m_set)
  927.                             {
  928.                                 changed = true;
  929.                                 tile->m_set = tile->neighbors[i]->m_set;
  930.                             }
  931.                     }
  932.                 }
  933.         }
  934.     }
  935.     catch(...)
  936.     {
  937.         debug.Print("Error in TERRAIN::CreateTileSets()");
  938.     }
  939. }
  940.  
  941. float H(INTPOINT a, INTPOINT b)
  942. {
  943.     return abs(a.x - b.x) + abs(a.y - b.y);
  944.     //return a.Distance(b);
  945. }
  946.  
  947. std::vector<INTPOINT> TERRAIN::GetPath(INTPOINT start, INTPOINT goal, bool considerUnits, MAPOBJECT *unit)
  948. {
  949.     try
  950.     {
  951.         //Check that the two points are within the bounds of the map
  952.         MAPTILE *startTile = GetTile(start);
  953.         MAPTILE *goalTile = GetTile(goal);
  954.         
  955.         if(!Within(start) || !Within(goal) || start == goal || startTile == NULL || goalTile == NULL)
  956.             return std::vector<INTPOINT>();
  957.  
  958.         //Check if a path exists
  959.         if(!startTile->m_walkable || !goalTile->m_walkable || startTile->m_set != goalTile->m_set)
  960.             return std::vector<INTPOINT>();
  961.  
  962.         //Check that goal tile isnt busy if considering units
  963.         if(considerUnits && goalTile->m_pMapObject != NULL)
  964.             goal = GetClosestFreeTile(goal, start);
  965.  
  966.         if(H(start, goal) < 10 && considerUnits && !PositionAccessible(unit, goal))
  967.             return std::vector<INTPOINT>();
  968.  
  969.         //Init Search
  970.         long numTiles = m_size.x * m_size.y;
  971.         for(long l=0;l<numTiles;l++)
  972.         {
  973.             m_pMapTiles[l].f = m_pMapTiles[l].g = INT_MAX;        //Clear F,G
  974.             m_pMapTiles[l].open = m_pMapTiles[l].closed = false;    //Reset Open and Closed
  975.         }
  976.  
  977.         std::vector<MAPTILE*> open;                //Create Our Open list
  978.         startTile->g = 0;                        //Init our starting point (SP)
  979.         startTile->f = H(start, goal);
  980.         startTile->open = true;
  981.         open.push_back(startTile);                //Add SP to the Open list
  982.  
  983.         bool found = false;                    // Search as long as a path hasnt been found,
  984.  
  985.         while(!found && !open.empty())        // or there is no more tiles to search
  986.         {    
  987.             MAPTILE * best = open[0];        // Find the best tile (i.e. the lowest F value)
  988.             int bestPlace = 0;
  989.             for(int i=1;i<open.size();i++)
  990.                 if(open[i] != NULL && open[i]->f < best->f)
  991.                 {
  992.                     best = open[i];
  993.                     bestPlace = i;
  994.                 }
  995.             
  996.             if(best == NULL)break;            //No path found
  997.             if(considerUnits && open.size() > 100)break;
  998.  
  999.             open[bestPlace]->open = false;
  1000.             open.erase(&open[bestPlace]);    // Take the best node out of the Open list
  1001.  
  1002.             if(best->m_mappos == goal)        //If the goal has been found
  1003.             {
  1004.                 std::vector<INTPOINT> p, p2;
  1005.                 MAPTILE *point = best;
  1006.  
  1007.                 while(point->m_mappos != start && point != NULL)    // Generate path
  1008.                 {
  1009.                     p.push_back(point->m_mappos);
  1010.                     point = point->m_pParent;
  1011.  
  1012.                     if(p.size() > 500)        //Too long path, something is wrong
  1013.                         return std::vector<INTPOINT>();
  1014.                 }
  1015.  
  1016.                 for(int i=p.size()-1;i!=0;i--)    // Reverse path
  1017.                     p2.push_back(p[i]);
  1018.                 p2.push_back(goal);
  1019.  
  1020.                 return p2;
  1021.             }
  1022.             else
  1023.             {
  1024.                 for(i=0;i<8;i++)                    // otherwise, check the neighbors of the
  1025.                     if(best->neighbors[i] != NULL)    // best tile
  1026.                         if(!considerUnits || best->neighbors[i]->m_pMapObject == NULL)
  1027.                         {
  1028.                             bool inList = false;        // Generate new G and F value
  1029.                             float newG = best->g + 1.0f;
  1030.                             float d = H(best->m_mappos, best->neighbors[i]->m_mappos);
  1031.                             float newF = newG + H(best->neighbors[i]->m_mappos, goal) + best->neighbors[i]->m_cost * 5.0f * d;
  1032.  
  1033.                             if(best->neighbors[i]->open || best->neighbors[i]->closed)
  1034.                             {
  1035.                                 if(newF < best->neighbors[i]->f)    // If the new F value is lower
  1036.                                 {
  1037.                                     best->neighbors[i]->g = newG;    // update the values of this tile
  1038.                                     best->neighbors[i]->f = newF;
  1039.                                     best->neighbors[i]->m_pParent = best;                                
  1040.                                 }
  1041.                                 inList = true;
  1042.                             }
  1043.  
  1044.                             if(!inList)            // If the neighbor tile isn't in the Open or Closed list
  1045.                             {
  1046.                                 best->neighbors[i]->f = newF;        //Set the values
  1047.                                 best->neighbors[i]->g = newG;
  1048.                                 best->neighbors[i]->m_pParent = best;
  1049.                                 best->neighbors[i]->open = true;
  1050.                                 open.push_back(best->neighbors[i]);    //Add it to the open list    
  1051.                             }
  1052.                         }
  1053.  
  1054.                 best->closed = true;        //The best tile has now been searched, add it to the Closed list
  1055.             }
  1056.         }
  1057.  
  1058.         return std::vector<INTPOINT>();        //No path found, return an empty path        
  1059.     }
  1060.     catch(...)
  1061.     {
  1062.         debug.Print("Error in TERRAIN::GetPath()");
  1063.         return std::vector<INTPOINT>();
  1064.     }
  1065. }
  1066.  
  1067. MAPTILE* TERRAIN::GetTile(int x, int y)
  1068. {
  1069.     if(m_pMapTiles == NULL)return NULL;
  1070.  
  1071.     try
  1072.     {
  1073.         if(Within(INTPOINT(x, y)))
  1074.             return &m_pMapTiles[x + y * m_size.x];
  1075.         else return NULL;
  1076.     }
  1077.     catch(...)
  1078.     {
  1079.         return NULL;
  1080.     }
  1081. }
  1082.  
  1083. void TERRAIN::SaveTerrain(char fileName[])
  1084. {
  1085.     try
  1086.     {
  1087.         std::ofstream out(fileName, std::ios::binary);        //Binary format
  1088.  
  1089.         if(out.good())
  1090.         {
  1091.             out.write((char*)&m_size, sizeof(INTPOINT));    //Write map size
  1092.  
  1093.             //Write all the maptile information needed to recreate the map
  1094.             for(int y=0;y<m_size.y;y++)
  1095.                 for(int x=0;x<m_size.x;x++)
  1096.                 {
  1097.                     MAPTILE *tile = GetTile(x, y);
  1098.                     out.write((char*)&tile->m_type, sizeof(int));            //type
  1099.                     out.write((char*)&tile->m_height, sizeof(float));        //Height
  1100.                 }
  1101.  
  1102.             //Write all the objects
  1103.             int numObjects = m_objects.size();
  1104.             out.write((char*)&numObjects, sizeof(int));     //Num Objects
  1105.             for(int i=0;i<m_objects.size();i++)
  1106.             {
  1107.                 out.write((char*)&m_objects[i].m_type, sizeof(int));                //type
  1108.                 out.write((char*)&m_objects[i].m_mappos, sizeof(INTPOINT));            //mappos
  1109.                 out.write((char*)&m_objects[i].m_meshInstance.m_pos, sizeof(D3DXVECTOR3));    //Pos
  1110.                 out.write((char*)&m_objects[i].m_meshInstance.m_rot, sizeof(D3DXVECTOR3));    //Rot
  1111.                 out.write((char*)&m_objects[i].m_meshInstance.m_sca, sizeof(D3DXVECTOR3));    //Sca
  1112.             }
  1113.         }
  1114.  
  1115.         out.close();
  1116.     }
  1117.     catch(...)
  1118.     {
  1119.         debug.Print("Error in TERRAIN::SaveTerrain()");
  1120.     }
  1121. }
  1122.  
  1123. void TERRAIN::LoadTerrain(char fileName[])
  1124. {
  1125.     try
  1126.     {
  1127.         std::ifstream in(fileName, std::ios::binary);        //Binary format
  1128.  
  1129.         if(in.good())
  1130.         {
  1131.             Release();    //Release all terrain resources
  1132.  
  1133.             in.read((char*)&m_size, sizeof(INTPOINT));    //read map size
  1134.         
  1135.             if(m_pMapTiles != NULL)    //Clear old maptiles
  1136.                 delete [] m_pMapTiles;
  1137.  
  1138.             //Create new maptiles
  1139.             m_pMapTiles = new MAPTILE[m_size.x * m_size.y];
  1140.             memset(m_pMapTiles, 0, sizeof(MAPTILE)*m_size.x*m_size.y);
  1141.  
  1142.  
  1143.             //Read the maptile information
  1144.             for(int y=0;y<m_size.y;y++)
  1145.                 for(int x=0;x<m_size.x;x++)
  1146.                 {
  1147.                     MAPTILE *tile = GetTile(x, y);
  1148.                     in.read((char*)&tile->m_type, sizeof(int));            //type
  1149.                     in.read((char*)&tile->m_height, sizeof(float));        //Height
  1150.                 }
  1151.  
  1152.             //Read number of objects
  1153.             int numObjects = 0;
  1154.             in.read((char*)&numObjects, sizeof(int));
  1155.             for(int i=0;i<numObjects;i++)
  1156.             {
  1157.                 int type = 0;
  1158.                 INTPOINT mp;
  1159.                 D3DXVECTOR3 p, r, s;
  1160.  
  1161.                 in.read((char*)&type, sizeof(int));            //type
  1162.                 in.read((char*)&mp, sizeof(INTPOINT));        //mappos
  1163.                 in.read((char*)&p, sizeof(D3DXVECTOR3));    //Pos
  1164.                 in.read((char*)&r, sizeof(D3DXVECTOR3));    //Rot
  1165.                 in.read((char*)&s, sizeof(D3DXVECTOR3));    //Sca
  1166.  
  1167.                 m_objects.push_back(OBJECT(type, mp, p, r, s));
  1168.             }
  1169.  
  1170.             //Recreate Terrain
  1171.             InitPathfinding();
  1172.             CreatePatches(3);
  1173.             CalculateAlphaMaps();
  1174.             CalculateLightMap(true);
  1175.         }
  1176.  
  1177.         in.close();
  1178.     }
  1179.     catch(...)
  1180.     {
  1181.         debug.Print("Error in TERRAIN::LoadTerrain()");
  1182.     }
  1183. }
  1184.  
  1185. D3DXVECTOR3 TERRAIN::GetWorldPos(INTPOINT mappos)
  1186. {
  1187.     if(!Within(mappos))return D3DXVECTOR3(0, 0, 0);
  1188.     MAPTILE *tile = GetTile(mappos);
  1189.     return D3DXVECTOR3(mappos.x, tile->m_height, -mappos.y);
  1190. }
  1191.  
  1192. INTPOINT TERRAIN::GetClosestFreeTile(INTPOINT to, INTPOINT from)
  1193. {
  1194.     MAPTILE *tile = GetTile(to);
  1195.     if(tile != NULL && tile->m_walkable && tile->m_pMapObject == NULL)
  1196.         return to;
  1197.  
  1198.     try
  1199.     {
  1200.         for(int i=0;i<3;i++)    //Search radius
  1201.         {
  1202.             std::vector<MAPTILE*> tiles;
  1203.  
  1204.             //Get tiles in current search radius
  1205.             for(int y=to.y - i;y<=to.y + i;y++)
  1206.             {
  1207.                 tiles.push_back(GetTile(to.x - i, y));
  1208.                 tiles.push_back(GetTile(to.x + i, y));
  1209.             }
  1210.  
  1211.             for(int x=to.x - i + 1;x<=to.x + i - 1;x++)
  1212.             {
  1213.                 tiles.push_back(GetTile(x, to.y - i));
  1214.                 tiles.push_back(GetTile(x, to.y + i));
  1215.             }
  1216.  
  1217.             //Find closest tile...
  1218.             MAPTILE *closest = NULL;
  1219.             float dist = 10000.0f;
  1220.  
  1221.             for(int t=0;t<tiles.size();t++)
  1222.                 if(tiles[t] != NULL)
  1223.                     if(tiles[t]->m_walkable && tiles[t]->m_pMapObject == NULL)
  1224.                     {
  1225.                         float d = from.Distance(tiles[t]->m_mappos);
  1226.                         if(d < dist)
  1227.                         {
  1228.                             dist = d;
  1229.                             closest = tiles[t];
  1230.                         }
  1231.                     }
  1232.  
  1233.             if(closest != NULL)
  1234.                 return closest->m_mappos;
  1235.         }
  1236.     }
  1237.     catch(...)
  1238.     {
  1239.         debug.Print("Error in TERRAIN::GetClosestFreeTile()");
  1240.     }
  1241.  
  1242.     return to;
  1243. }
  1244.  
  1245. bool TERRAIN::PositionAccessible(MAPOBJECT *unit, INTPOINT position)
  1246. {
  1247.     if(unit == NULL || !Within(unit->m_mappos) || !Within(position))return false;
  1248.  
  1249.     //Set start and end location of the search
  1250.     INTPOINT start = unit->m_mappos;
  1251.     INTPOINT end = position;
  1252.     if(start.x > end.x)std::swap(start.x, end.x);
  1253.     if(start.y > end.y)std::swap(start.y, end.y);
  1254.  
  1255.     if(H(start, end) >= 9)
  1256.         return true;
  1257.  
  1258.     //Set the open variable of search area's mapTiles to false
  1259.     int searchBorder = 3;
  1260.     for(int y=start.y - searchBorder;y<=end.y + searchBorder;y++)
  1261.         for(int x=start.x - searchBorder;x<=end.x + searchBorder;x++)
  1262.             if(Within(INTPOINT(x, y)))
  1263.                 m_pMapTiles[y * m_size.x + x].open = false;
  1264.  
  1265.     //Set the open variable of the start location to true
  1266.     m_pMapTiles[position.y * m_size.x + position.x].open = true;
  1267.     searchBorder--;
  1268.  
  1269.     //Iterate any open nodes to its neighbors if they are unoccupied by units
  1270.     bool changed;
  1271.  
  1272.     do
  1273.     {
  1274.         changed = false;
  1275.  
  1276.         for(int y=start.y - searchBorder;y<=end.y + searchBorder;y++)
  1277.             for(int x=start.x - searchBorder;x<=end.x + searchBorder;x++)
  1278.                 if(Within(INTPOINT(x, y)) && !m_pMapTiles[y * m_size.x + x].open)
  1279.                     for(int i=0;i<8;i++)
  1280.                     {
  1281.                         MAPTILE *t = m_pMapTiles[y * m_size.x + x].neighbors[i];
  1282.                         if(t != NULL && t->open && (t->m_pMapObject == NULL || t->m_pMapObject == unit))
  1283.                         {
  1284.                             m_pMapTiles[y * m_size.x + x].open = true;
  1285.                             changed = true;
  1286.                             i = 8;
  1287.                         }
  1288.                     }
  1289.  
  1290.         //If the open variable of the end node is true then a path exists
  1291.         if(m_pMapTiles[unit->m_mappos.y * m_size.x + unit->m_mappos.x].open)
  1292.             return true;
  1293.     }
  1294.     while(changed);
  1295.     
  1296.     //No path exists
  1297.     return false;
  1298. }